import Vue from "vue";
import VueRouter from "vue-router";
// import Login from "../views/Login";
import Home from "../views/Home";
import Room from "../views/Room";
import RoomType from "../views/RoomType";
import Login from "../views/Login";
import CheckInfo from "../views/CheckIn/CheckInfo";
import Customer from "../views/CheckIn/Customer";
import RoomState from "../views/RoomState";
import Discount from "../views/Discount";
import RoomTypeDiscount from "../views/RoomTypeDiscount";
import CheckOut from "../views/CheckOut";
import Dashboard from "../views/Dashboard";
import ReserveManag from "../views/ReserveManag";

import store from "@/store";
import storage from "@/model/storage";

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    name: "Login",
    component: Login
  },
  {
    path: "/Home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/ReserveManag",
        name: "ReserveManag",
        component: ReserveManag,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/RoomState",
        name: "RoomState",
        component: RoomState,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Room",
        name: "Room",
        component: Room,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/RoomType",
        name: "RoomType",
        component: RoomType,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Discount",
        name: "Discount",
        component: Discount,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/RoomTypeDiscount",
        name: "RoomTypeDiscount",
        component: RoomTypeDiscount,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/CheckOut",
        name: "CheckOut",
        component: CheckOut,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/CheckInfo",
        name: "CheckInfo",
        component: CheckInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Customer",
        name: "Customer",
        component: Customer,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Dashboard",
        name: "Dashboard",
        component: Dashboard,
        meta: {
          requireAuth: true
        }
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error);
};
// 设置路由守卫，在进页面之前，判断有token，才进入页面，否则返回登录页面
if (storage.get("token")) {
  var token = storage.get("token").replaceAll('"', "");
  store.commit("setToken", token);
  // store.commit("setToken", sessionStorage.getItem("token"));
}
router.beforeEach((to, from, next) => {
  // 判断要去的路由有没有requiresAuth
  // to.matched.some(r => r.meta.requireAuth) or to.meta.requiresAuth
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.token) {
      next(); //有token,进行request请求，后台还会验证token
    } else {
      next({
        path: "/",
        // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由，这要进一步在登陆页面判断
        query: { redirect: to.fullPath }
      });
    }
  } else {
    next(); //如果无需token,那么随它去吧
  }
});
export default router;
